﻿using System;
using System.Linq;

using Data.GameData;

namespace GUIDialogs
{
    public partial class GameSelection : BaseDialog
    {
        private bool loadGame;

        public GameSelection(bool loadGame)
        {
            InitializeComponent();

            this.loadGame = loadGame;
            InitialiseDialog();
        }

        /// <summary>
        /// Setup the initial values for the dialog.
        /// </summary>
        protected void InitialiseDialog()
        {
            gameListBox.Items.AddRange(Game.Games.OrderBy(game => game.Name).ToArray());

            gameNameTextBox.ReadOnly = loadGame;
        }

        /// <summary>
        /// The Game selected by the player
        /// </summary>
        public Game SelectedGame
        {
            get { return gameListBox.SelectedItem as Game; }
        }

        /// <summary>
        /// Method to be overridden in derived classes.
        /// Determine is the manditory data is correct on the form
        /// </summary>
        /// <returns>True if the data is correct, false if otherwise</returns>
        protected override bool IsValid()
        {
            errorProvider.SetError(gameNameTextBox, "");

            bool result = true;
            if (!loadGame && gameNameTextBox.Text.Trim().Length == 0)
            {
                errorProvider.SetError(gameNameTextBox, "A game must have a name.");
                result = false;
            }

            return result;
        }

        /// <summary>
        /// Fill the Game summary info based on the selected option
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Not used.</param>
        private void gameListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Game selected = gameListBox.SelectedItem as Game;

            if (selected != null)
            {
                gameNameTextBox.Text = selected.Name;
                turnTextBox.Text = selected.TurnNumber.ToString();
                dayTextBox.Text = selected.CurrentDay.ToString();
            }
            else
            {
                gameNameTextBox.Text = "";
                turnTextBox.Text = "0";
                dayTextBox.Text = "0";
            }
        }
    }
}
